package com.gcb.service.impl;


import cn.hutool.json.JSONArray;
import com.gcb.domain.entity.Region;
import com.gcb.domain.vo.RegionVO;
import com.gcb.mapper.RegionMapper;
import com.gcb.service.IRegionService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RegionServiceImpl implements IRegionService {

    @Resource
    private RegionMapper regionMapper;


    @Override
    @Cacheable(cacheNames = "regionTree", key = "'regionTree_'+#level")
    public List<RegionVO> getRegionTree(Integer level) {
        List<Region> regions = regionMapper.selectAll();

        List<RegionVO> roots = new ArrayList<>();

        for (Region region : regions) {
            RegionVO provinceNode = findNodeByCode(roots, region.getProvinceGeocode());

            // 根据 level 进行处理
            switch (level) {
                case 1: // 只显示省
                    if (provinceNode == null) {
                        provinceNode = new RegionVO(region.getProvince(), region.getProvinceGeocode());
                        roots.add(provinceNode);
                    }
                    break;
                case 2: // 显示省市
                    if (provinceNode == null) {
                        provinceNode = new RegionVO(region.getProvince(), region.getProvinceGeocode());
                        roots.add(provinceNode);
                    }
                    RegionVO cityNode = findNodeByCode(provinceNode.getChild(), region.getCityGeocode());
                    if (cityNode == null) {
                        cityNode = new RegionVO(region.getCity(), region.getCityGeocode());
                        provinceNode.addChild(cityNode);
                    }
                    break;
                case 3: // 显示省市区
                    if (provinceNode == null) {
                        provinceNode = new RegionVO(region.getProvince(), region.getProvinceGeocode());
                        roots.add(provinceNode);
                    }
                    cityNode = findNodeByCode(provinceNode.getChild(), region.getCityGeocode());
                    if (cityNode == null) {
                        cityNode = new RegionVO(region.getCity(), region.getCityGeocode());
                        provinceNode.addChild(cityNode);
                    }
                    RegionVO districtNode = findNodeByCode(cityNode.getChild(), region.getDistrictGeocode());
                    if (districtNode == null) {
                        districtNode = new RegionVO(region.getDistrict(), region.getDistrictGeocode());
                        cityNode.addChild(districtNode);
                    }
                    break;
                default:
                    // 如果 level 不合法，则抛出异常或返回默认值
                    throw new IllegalArgumentException("Invalid level value: " + level);
            }
        }
        return roots;
    }

    @Override
    public JSONArray getCityCode(String city) {
        Region byCity = regionMapper.getByCity(city);
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(byCity.getProvinceGeocode());
        jsonArray.add(byCity.getCityGeocode());
        return jsonArray;
    }

    private static RegionVO findNodeByCode(List<RegionVO> nodes, String code) {
        if (nodes == null || nodes.isEmpty()) {
            return null;
        } else {
            for (RegionVO node : nodes) {
                if (node.getCode().equals(code)) {
                    return node;
                }
            }
        }
        return null;
    }
}
